﻿using Eps.Ai.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Media;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;

namespace InfonavaEye.util
{
    public class SoundHelper
    {
        private static SoundPlayer soundPlayer = new SoundPlayer();
        private static string baseDir = FileHelper.getBaseDir();

        [DllImport("Winmm.dll")]
        private static extern int waveOutSetVolume(int hwo, System.UInt32 pdwVolume);

        [DllImport("Winmm.dll")]
        private static extern uint waveOutGetVolume(int hwo, out System.UInt32 pdwVolume);
       
        private static int volumeMinScope = 0;
        private static int volumeMaxScope = 100;

        /// <summary>
        /// 设置音量（0~100）
        /// </summary>
        /// <param name="volume"></param>
        public static void SetVolume(int volume)
        {
            if (volume < 0)
            {
                volume = 0;
            }

            if (volume > 100)
            {
                volume = 100;
            }
            System.UInt32 Value = (System.UInt32)((double)0xffff * (double)volume / (double)(volumeMaxScope - volumeMinScope));//先把trackbar的value值映射到0x0000～0xFFFF范围


            //限制value的取值范围
            if (Value < 0)
            {
                Value = 0;
            }

            if (Value > 0xffff)
            {
                Value = 0xffff;
            }

            System.UInt32 left = (System.UInt32)Value;//左声道音量
            System.UInt32 right = (System.UInt32)Value;//右
            waveOutSetVolume(0, left << 16 | right); //"<<"左移，“|”逻辑或运算
        }

        /// <summary>
        /// 播放音频
        /// </summary>
        /// <param name="audioName"></param>
        public static void PlayAudio(string audioName)
        {
            try
            {
                SetVolume(Config.AUDIO_VOLUME);
                soundPlayer.Stop();
                soundPlayer.SoundLocation = baseDir + "\\Resources\\audio\\" + audioName;
                soundPlayer.Play();
            }
            catch (Exception ex)
            {
                Console.WriteLine("播放声音发生异常：{0}，{1}", ex.Message, ex.StackTrace);
            }
        }

    }
}
